﻿using CommonTools;
using shop.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shop.Bll.cost
{
    public class ShuadanBll:IPageBll<Shuadan>
    {
        public string Title => "刷单费用";

        public bool EnableEditOnList => true;

        public bool EnableCreateOnList => true;

        public bool RefreshOnOpen => true;

        public Action<int> TurnPage { set; get; }

        public Shuadan CopyItem(Shuadan item)
        {
            if (item == null)
                return null;

            var newItem = item.Clone();
            newItem.id = 0;
            newItem.isdel = false;

            return newItem;
        }

        public Shuadan CreateItem()
        {
            return new Shuadan() { isdel = false, FUser = UserBll.CurrentUser.Name, FTime = DateTime.Now,};
        }

        public async Task<Result> DeleteItem(int id)
        {
            await DB.FSql.Update<Shuadan>()
                .Where(a => a.id == id && a.isdel == false)
                .Set(a => a.isdel, true)
                .ExecuteAffrowsAsync();

            await DB.FSql.Insert(new CLogs()
            {
                DocName = nameof(Shuadan),
                Computer = UserBll.CurrentUser.ComputerInfo,
                Zdr = UserBll.CurrentUser.Name,
                DocId = id.ToString(),
                DocAction = "删除",
            })
                .ExecuteAffrowsAsync();

            return new Result(true);
        }


        public async Task<Result<int>> EditCell(string dataPropertyName, object value,  Shuadan data)
        {
            if (data.id == 0)
            {
                data.FTime = await DB.FSql.GetTimeAsync();
                data.FUser = UserBll.CurrentUser.Name;
                data.isdel = false;

                var nid = await DB.FSql.Insert(data)
                    .ExecuteIdentityAsync();

                data.id = (int)nid;
            }

            var dict = new Dictionary<string, object> { [dataPropertyName] = value };


            //switch (dataPropertyName)
            //{
            //    case nameof(Shuadan.Flength):
            //    case nameof(Shuadan.Width):
            //    case nameof(Shuadan.Height):
            //        data.weightRatio = Math.Max(data.Width * data.Flength * data.Height / 10000, data.FWeight);
            //        data.weightRatio8000 = Math.Max(data.Width * data.Flength * data.Height / 8000, data.FWeight);
            //        dict.Add(nameof(data.weightRatio), data.weightRatio);
            //        dict.Add(nameof(data.weightRatio8000), data.weightRatio8000);
            //        break;
            //    default:
            //        break;
            //}


            var dbRes = await DB.FSql.Update<Shuadan>()
                .Where(a => a.id == data.id && a.isdel == false)
                .SetDto(dict)
                .ExecuteAffrowsAsync();

            return new Result<int>(true, "!", data.id);
        }

        public IEnumerable<DeclareColumn> GetColumns()
        {
            return new[]
            {
                new DeclareColumn(nameof(Shuadan.id),"id"){ Visible= false},
                new DeclareColumn(nameof(Shuadan.shopGroup),"店铺分类"),
                new DeclareColumn(nameof(Shuadan.MinAmount),"最小值"){ ValueType= typeof(decimal)},
                new DeclareColumn(nameof(Shuadan.MaxAmount),"最大值"){ ValueType= typeof(decimal)},
                new DeclareColumn(nameof(Shuadan.Fee),"手续费"),
                new DeclareColumn(nameof(Shuadan.FUser),"制单人"),
            };
        }

        public async Task<Shuadan> GetItem(int id)
        {
            return await DB.FSql.Select<Shuadan>()
                .Where(a => a.isdel == false && a.id == id)
                .ToOneAsync();
        }


        #region 查询参数

        private DateTime? _begin;
        private DateTime? _end;
        private string _shopGroup;

        #endregion

        /// <summary> 查询ui </summary>
        public virtual ControlBuilder GetUIBuilder()
        {
            return new ControlBuilder()
                //.Add<DateTime?>(ControlEnum.gtDatePicker, "起始日期", c => _begin = c)
                //.Add<DateTime?>(ControlEnum.gtDatePicker, "截止日期", c => _end = c)
                .Add<string>(ControlEnum.guanTextBox, "查找", c => _shopGroup = c)
                .AddQueryButton("查询");
        }

        /// <summary> 查询 </summary>
        public async Task<PageQueryResult<Shuadan>> QueryPage(DocFilterParameter queryPara)
        {
            var list = await DB.FSql.Select<Shuadan>()
                .Where(a => a.isdel == false)
                //.WhereIf(_begin != null, a => a.BeginDate <= _begin.Value.Date && a.EndDate>= _begin.Value.Date)
                //.WhereIf(_end != null, a => a.BeginDate <= _end.Value.Date && a.EndDate>= _end.Value.Date)
                .WhereIf(!string.IsNullOrEmpty(_shopGroup), a => a.shopGroup.Contains(_shopGroup))
                .Page(queryPara.PageNo, queryPara.PageSize)
                //.OrderByDescending(a => a.shopGroup)
                .OrderBy(a => a.id)
                .Count(out var c)
                .ToListAsync();

            return new PageQueryResult<Shuadan>(true, null, list, c);
        }



        public async Task<Result> SaveItem(Shuadan item)
        {
            if (item == null)
            {
                return new Result(false, "系统错误");
            }

            string err = string.Empty;
            //if (string.IsNullOrEmpty(item.shopGroup))
            //{
            //    err += "货品编号不能为空!";
            //}

            if (string.IsNullOrEmpty(err) == false)
            {
                return new Result(false, err);
            }

            item.isdel = false;
            item.FTime = await DB.FSql.GetTimeAsync();
            item.FUser = UserBll.CurrentUser.Name;

            if (item.id <= 0)
            {
                var id = await DB.FSql.Insert(item)
                    .ExecuteIdentityAsync();

                item.id = (int)id;
            }
            else
            {
                await DB.FSql.Update<Shuadan>()
                    .SetSource(item)
                    .ExecuteAffrowsAsync();
            }

            return new Result(true);
        }


        public async Task<Result> SaveItem(IEnumerable<Shuadan> items)
        {
            if (items == null)
            {
                return new Result(false, "系统错误");
            }

            var time = await DB.FSql.GetTimeAsync(); 

            foreach (var item in items)
            {
                item.isdel = false;
                item.FTime = time;
                item.FUser = UserBll.CurrentUser.Name;
            }

            await DB.FSql.Insert(items).ExecuteAffrowsAsync();

            return new Result(true);

        }


    }
}
